package cn.ingrun.cloud_disk.controller;


import cn.ingrun.cloud_disk.auth.authService.AuthService;
import cn.ingrun.cloud_disk.model.ResponseJson;
import cn.ingrun.cloud_disk.model.User;
import cn.ingrun.cloud_disk.service.UserService;
import cn.ingrun.cloud_disk.utils.MyResponse;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);
    private UserService userService;
    private AuthService authService;

    @Autowired
    public void setAuthService(AuthService authService) {
        this.authService = authService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("api/login")
    public MyResponse login(@RequestBody JSONObject loginInfo){

        if (!(loginInfo.containsKey("username") || loginInfo.containsKey("password"))){
            return MyResponse.error("用户名或密码不存在！");
        }
        String id = (String) loginInfo.get("id");
        String password = (String) loginInfo.get("password");

        ResponseJson responseJson = authService.login(id,password);

        logger.info("登陆成功：" + responseJson.toString());
        return MyResponse.success(responseJson);
    }


    @PostMapping("api/register")
    public MyResponse register(@RequestBody User user){
        ResponseJson responseJson = userService.register(user);
        if (responseJson.getCode() == 1){
            return MyResponse.success(responseJson);
        }
        return MyResponse.error(responseJson);
    }

}
